#include "CBuffData.h"
#include "TinyXML.h"
#include "UtilityFunctions.h"



CBuffData::CBuffData( void )
{
}

CBuffData::~CBuffData( void )
{
}

BOOL CBuffData::OnLoadBuffData( std::tstring strFileName )
{
	if( strFileName.length() <= 0 ) return FALSE;

	TiXmlDocument Doc;
#ifdef UNICODE
	std::string strAnsiFileName = CodeUtil::UniCodeToAnsi( strFileName.c_str() );
	if( !Doc.LoadFile( strAnsiFileName.c_str() ) ) return FALSE;
#else
	if( !Doc.LoadFile( strFileName.c_str() ) ) return FALSE;
#endif

	TiXmlNode* pRoot = Doc.FirstChild( "BuffDataList" );
	if( !pRoot ) return FALSE;

	TiXmlNode* pBuffData = pRoot->FirstChild( "BuffData" );
	while( pBuffData )
	{
		stBuffData NewBuffData;

		int nBuffID = atoi( pBuffData->ToElement()->Attribute( "ID" ) );
		std::tstring strBuffName = CodeUtil::ConvertStringToWString( pBuffData->ToElement()->Attribute( "Name" ) );
		eBuffType eType = ( eBuffType )atoi( pBuffData->ToElement()->Attribute( "Type" ) );
		__int64 nTimeDuration = atoi( pBuffData->ToElement()->Attribute( "Duration" ) );
		float fValue = ( float )atof( pBuffData->ToElement()->Attribute( "Value" ) );
		
		AddBuff( nBuffID, strBuffName, eType, nTimeDuration, fValue );
		pBuffData = pBuffData->NextSibling();
	}

	return TRUE;
}

BOOL CBuffData::OnSaveBuffData( std::tstring strFileName )
{
	if( strFileName.length() <= 0 ) return FALSE;

	TiXmlDocument Doc;
	TiXmlDeclaration* pDec = new TiXmlDeclaration( "1.0", "ks_c_5601-1987", "" );
	Doc.LinkEndChild( pDec );

	TiXmlElement* pRoot = new TiXmlElement( "BuffDataList" );
	Doc.LinkEndChild( pRoot );

	int nBuffCount = m_mapBuffData.GetSize();
	for( int nCount = 0 ; nCount < nBuffCount ; ++nCount )
	{
		stBuffData* pData = m_mapBuffData.GetByIndex( nCount );
		if( pData )
		{
			TiXmlElement* pBuff = new TiXmlElement( "BuffData" );
			if( pBuff )
			{
				pBuff->SetAttribute( "ID", pData->m_nBuffID );
#ifdef UNICODE
				std::string strName = CodeUtil::UniCodeToAnsi( pData->m_strBuffName.c_str() );
				pBuff->SetAttribute( "Name", strName.c_str() );
#else
				pBuff->SetAttribute( "Name", pData->m_strBuffName.c_str() );
#endif
				pBuff->SetAttribute( "Type", pData->m_eType );
				pBuff->SetAttribute( "Duration", ( int )pData->m_nTimeDuration );

				std::string strValue = CodeUtil::FloatToString( pData->m_fValue );
				pBuff->SetAttribute( "Value", strValue.c_str() );

				pRoot->LinkEndChild( pBuff );
			}			
		}
	}

#ifdef UNICODE
	std::string strAnsiFileName = CodeUtil::UniCodeToAnsi( strFileName.c_str() );
	Doc.SaveFile( strAnsiFileName.c_str() );
#else
	Doc.SaveFile( strFileName.c_str() );
#endif
	return TRUE;
}

int CBuffData::GetBuffIDByIndex( int nIndex )
{
	stBuffData* pData = m_mapBuffData.GetByIndex( nIndex );
	return pData ? pData->m_nBuffID : -1;
}

int CBuffData::GetBuffIDByName( std::tstring strName )
{
	int* pID = m_mapSearchByName.Get( strName );
	return pID ? *pID : -1;
}

eBuffType CBuffData::GetBuffType( int nBuffID )
{
	stBuffData* pData = _GetBuffData( nBuffID );
	return pData ? pData->m_eType : BuffType_UnKnown;
}

__int64 CBuffData::GetBuffDuration( int nBuffID )
{
	stBuffData* pData = _GetBuffData( nBuffID );
	return pData ? pData->m_nTimeDuration : 0;
}

float CBuffData::GetBuffValue( int nBuffID )
{
	stBuffData* pData = _GetBuffData( nBuffID );
	return pData ? pData->m_fValue : 0.0f;
}

const TCHAR* CBuffData::GetBuffName( int nBuffID )
{
	stBuffData* pData = _GetBuffData( nBuffID );
	return pData ? pData->m_strBuffName.c_str() : NULL;
}

void CBuffData::AddBuff( int nBuffID, std::tstring strName, eBuffType eType, __int64 nDuration, float fValue )
{
	stBuffData* pData = _GetBuffData( nBuffID );
	if( pData )
	{
		pData->m_strBuffName = strName;
		pData->m_eType = eType;
		pData->m_nTimeDuration = nDuration;
		pData->m_fValue = fValue;
	}
	else
	{
		stBuffData NewBuffData;

		NewBuffData.m_nBuffID = nBuffID;
		NewBuffData.m_strBuffName = strName;
		NewBuffData.m_eType = eType;
		NewBuffData.m_nTimeDuration = nDuration;
		NewBuffData.m_fValue = fValue;

		m_mapBuffData.Add( nBuffID, NewBuffData );
		m_mapSearchByName.Add( strName, nBuffID );
	}
}

void CBuffData::DeleteBuff( int nBuffID )
{
	const TCHAR* pName = GetBuffName( nBuffID );
	if( pName && _tcslen( pName ) > 0 )
	{
		m_mapSearchByName.Delete( pName );
	}

	m_mapBuffData.Delete( nBuffID );
}

void CBuffData::SetBuffName( int nBuffID, std::tstring strName )
{
	stBuffData* pData = _GetBuffData( nBuffID );
	if( pData )
	{
		pData->m_strBuffName = strName;
	}
}

void CBuffData::SetBuffType( int nBuffID, eBuffType eType )
{
	stBuffData* pData = _GetBuffData( nBuffID );
	if( pData )
	{
		pData->m_eType = eType;
	}
}

void CBuffData::SetBuffDuration( int nBuffID, __int64 nDuration )
{
	stBuffData* pData = _GetBuffData( nBuffID );
	if( pData )
	{
		pData->m_nTimeDuration = nDuration;
	}
}

void CBuffData::SetBuffValue( int nBuffID, float fValue )
{
	stBuffData* pData = _GetBuffData( nBuffID );
	if( pData )
	{
		pData->m_fValue = fValue;
	}
}

stBuffData* CBuffData::_GetBuffData( int nBuffID )
{
	return m_mapBuffData.Get( nBuffID );
}
